package com.pig4cloud.pig.module.service.examine.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.module.api.entity.examine.*;
import com.pig4cloud.pig.module.mapper.examine.ExamineApproveTemplateMapper;
import com.pig4cloud.pig.module.mapper.examine.ExamineApproveTemplateUserMapper;
import com.pig4cloud.pig.module.service.examine.IExamineApproveTemplateService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 审批流程模板Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-23
 */
@Service
public class ExamineApproveTemplateServiceImpl extends ServiceImpl<ExamineApproveTemplateMapper, ExamineApproveTemplate> implements IExamineApproveTemplateService {

	@Resource
	private ExamineApproveTemplateMapper approveTemplateMapper;
	@Resource
	private ExamineApproveTemplateUserMapper approveTemplateUserMapper;

	@Override
	public TemplateTree getTreeByTemplateId(ExamineApproveTemplate approveTemplate) {
		TemplateTree tree = new TemplateTree();
		tree.setRootId("root_" + approveTemplate.getId());
		QueryWrapper<ExamineApproveTemplate> qw = new QueryWrapper<>();
		qw.eq("id",approveTemplate.getId());
//		qw.eq("parent_id",0);
		List<ExamineApproveTemplate> groups = approveTemplateMapper.selectList(qw);
		List<TemplateTreeNode> nodes = getNodes(groups,tree);
		List<TemplateTreeLink> links = getLinks(groups);
		tree.setLinks(links);
		tree.setNodes(nodes);
		return tree;
	}



	/**
	 * 获取所有的 架构树的 节点
	 * @param templates
	 * @return
	 */
	private List<TemplateTreeNode> getNodes(List<ExamineApproveTemplate> templates, TemplateTree tree){
		List<TemplateTreeNode> nodes = new ArrayList<>();
		if(templates != null) {
			for (ExamineApproveTemplate template : templates) {

				TemplateTreeNode nodeTemplate = new TemplateTreeNode();
				nodeTemplate.setId("group_" + template.getId().toString());

				NodeData nodeDataGroup = new NodeData();
				nodeDataGroup.setType("template");
				nodeDataGroup.setTId(template.getId().toString());
				nodeTemplate.setData(nodeDataGroup);

				nodeTemplate.setText(template.getTemplateName());
				nodeTemplate.setWidth(120);
				nodeTemplate.setHeight(90);
				nodeTemplate.setColor("rgba(30, 144, 255, 1)");
				nodes.add(nodeTemplate);


				QueryWrapper<ExamineApproveTemplateUser> qwUser = new QueryWrapper<>();
				qwUser.eq("template_id",template.getId());
				qwUser.orderByAsc("duty_index");
				List<ExamineApproveTemplateUser> users = approveTemplateUserMapper.selectList(qwUser);
				if(CollUtil.isNotEmpty(users)){
					int index = 1;
					for (ExamineApproveTemplateUser user : users){

						TemplateTreeNode nodeUser = new TemplateTreeNode();
						nodeUser.setId("user_" + user.getId().toString());

						NodeData nodeDataUser = new NodeData();
						nodeDataUser.setType("user");
						nodeDataUser.setTId(user.getId().toString());
						nodeDataUser.setTemplateId(user.getTemplateId());
						nodeDataUser.setTemplateUserId(user.getUserId());
						nodeDataUser.setDuty("职责：" + user.getDutyName());
						nodeUser.setData(nodeDataUser);
						nodeUser.setText(user.getUserName());
						nodeUser.setWidth(120);
						nodeUser.setHeight(90);
						nodeUser.setColor("rgba(30, 144, 255, 1)");
						nodes.add(nodeUser);
						index ++;
					}
				}

			}
		}
		return nodes;
	}

	/**
	 * 获取架构树的 连接线
	 * @param templates
	 * @return
	 */
	private List<TemplateTreeLink> getLinks(List<ExamineApproveTemplate> templates){
		List<TemplateTreeLink> links = new ArrayList<>();
		if(templates != null){
			ExamineApproveTemplate first = null;
			for (ExamineApproveTemplate template : templates) {

				TemplateTreeLink linkGroup = new TemplateTreeLink();
				linkGroup.setFrom("group_"+template.getId());
				QueryWrapper<ExamineApproveTemplateUser> qw = new QueryWrapper<>();
				qw.eq("template_id",template.getId());
				List<ExamineApproveTemplateUser> users = approveTemplateUserMapper.selectList(qw);
				if(CollUtil.isNotEmpty(users)){
					for (ExamineApproveTemplateUser user : users){
						TemplateTreeLink linkUser = new TemplateTreeLink();
						linkUser.setFrom("user_" + user.getId());
						for (ExamineApproveTemplateUser user1 : users){
							if (user1.getDutyIndex()==user.getDutyIndex()+1){
								linkUser.setTo("user_" + user1.getId().toString());
							}
							if (user1.getDutyIndex()==0){
								linkGroup.setTo("user_" + user1.getId());
							}
						}

						links.add(linkUser);
					}
					links.add(linkGroup);
				}


			}
		}
		return  links;
	}
}
